package com.juntuo.listener;

import com.github.pagehelper.StringUtil;
import com.juntuo.cache.LocalCache;
import com.juntuo.entity.DeviceHistory;
import com.juntuo.service.DeviceHistoryService;
import com.juntuo.youbo.AuthenticationUtil4Youbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

public class WebStartupListener implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(WebStartupListener.class);

    @Autowired
    private DeviceHistoryService deviceHistoryService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
//        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
            logger.info("------ 系统启动完成,开始添加缓存 ------");
//            List<DeviceHistory> histories = deviceHistoryService.selectGroupedLastRecord();
//            LocalCache cache = new LocalCache();
//            for(DeviceHistory history:histories){
//                cache.putValue(history.getDeviceId(),history);
//                System.out.println("cache:"+cache.getValue(history.getDeviceId()));
//            }
            try {
                String token =AuthenticationUtil4Youbo.login();
//                if(StringUtil.isEmpty(token)){
                    //todo 通知
//                }
                LocalCache.putTokenValue("ybAccessToken",token);
            } catch (Exception e) {
                e.printStackTrace();
            }
        logger.info("------ 缓存添加完成 ------");
//            logger.info(userService.queryUserByName("王八"));
//        }
    }
}
